package com.ywwxhz.plugins.eyecare;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.WindowManager;

import com.ywwxhz.Interface.IModleService;
import com.ywwxhz.Util.PreferencesKit;
import com.ywwxhz.taskmgr.R;

/**
 * Created by ywwxhz on 2014/10/6.
 */
public class EyesCareModel implements IModleService {

    public static final int VIEW_ENABLE = 1;
    public static final int VIEW_DISABLE = 2;
    public static final int VIEW_AUTO_ENABLE = 3;
    public static final int VIEW_AUTO_DISABLE = 4;
    public static final int VIEW_UPDATE_CONFIG = 5;
    public static final int MODEL = 4;
    public static final String PARAM = "EyesCare_PARAM";
    private static final String TAG = EyesCareModel.class.getSimpleName();
    private static EyesCareModel instance;
    private Context context;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mLayoutParams;
    private boolean eyeCare;
    private EyesCareView mEyesCareView;

    public EyesCareModel(Context context) {
        this.context = context;
        onRecover(context);
    }

    public static EyesCareModel getInstance(Context context) {
        if (instance == null) {
            instance = new EyesCareModel(context);
        }
        return instance;
    }

    /**
     * 创建置顶窗体
     */
    private void createWindowManager() {
        // 取得系统窗体
        mWindowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        // 窗体的布局样式
        mLayoutParams = new WindowManager.LayoutParams();

        // 设置窗体显示类型——TYPE_SYSTEM_OVERLAY(系统提示)
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;

        // 设置窗体焦点及触摸：
        // FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        // 设置显示的模式
        mLayoutParams.format = PixelFormat.RGBA_8888;

        // 设置对齐的方法
        mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;

        // 设置窗体宽度和高度
        mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

        // 设置窗体显示的位置，否则在屏幕中心显示
        mLayoutParams.x = 0;
        mLayoutParams.y = 0;
    }

    private void showEyeCareView(boolean enable) {
        if (enable) {
            if (!eyeCare) {
                if (mWindowManager == null) {
                    createWindowManager();
                }
                /**
                 * 创建悬浮窗体
                 */
                if (mEyesCareView == null) {
                    mEyesCareView = new EyesCareView(context);
                }
                notifyAlphaSettingChange();
                mEyesCareView.show();

                try {
                    mWindowManager.addView(mEyesCareView, mLayoutParams);
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        } else {
            if (mEyesCareView != null) {
                mEyesCareView.setAnimatorListener(new EyesCareView.AnimatorListener() {
                    @Override
                    public void onShowEnd() {
                    }

                    @Override
                    public void ondismissEnd() {
                        try {
                            mWindowManager.removeView(mEyesCareView);
                        } catch (Exception ignored) {
                            ignored.printStackTrace();
                        }
                    }
                });
                mEyesCareView.dismiss();

            }
        }
        eyeCare = enable;
    }

    @Override
    public void execute(Intent intent) {
        switch (intent.getIntExtra(PARAM, 0)) {
            case VIEW_ENABLE:
                showEyeCareView(true);
                break;
            case VIEW_DISABLE:
                showEyeCareView(false);
                break;
            case VIEW_AUTO_ENABLE:
                mEyesCareView.show();
                break;
            case VIEW_AUTO_DISABLE:
                mEyesCareView.dismiss();
                break;
            case VIEW_UPDATE_CONFIG:
                notifyAlphaSettingChange();
                break;
            default:
                break;
        }
    }

    public EyesCareView getmEyesCareView() {
        return mEyesCareView;
    }

    @Override
    public void onConfigChange(Configuration newConfig) {
    }

    @Override
    public void onDestroy() {
        context = null;
        instance = null;
    }

    @Override
    public boolean getStatus() {
        return eyeCare;
    }

    @Override
    public void onRecover(Context context) {
        showEyeCareView(PreferencesKit.getboolean(context,
                context.getString(R.string.pref_eyecare_key), false));
    }

    public void notifyAlphaSettingChange() {
        mEyesCareView.setEndAlpha(PreferencesKit.getFloat(context,
                context.getResources().getString(R.string.pref_eyecare_alpha_key),
                0.5f));
    }

}
